package com.iotimc.attendancesystem.attendance;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.attendance.videmodel.AttendanceViewModel;
import com.iotimc.attendancesystem.base.BaseDataBindingFragment;
import com.iotimc.attendancesystem.base.BaseFragment;
import com.iotimc.attendancesystem.databinding.FragmentVerification2Binding;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.attendancesystem.util.PictureManager;
import com.pefis.view.FaceDetectionView;
import com.pefis.view.FrameView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author david
 * @date 2017/11/27
 */

public class AttendanceFragment2 extends BaseDataBindingFragment<FragmentVerification2Binding>
        implements AttendanceContract.View {

    AttendanceContract.Presenter presenter;

    @Override
    protected int getResId() {
        return R.layout.fragment_verification2;
    }

    int count = 0;
    private boolean add;
    AttendanceViewModel attendanceViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        attendanceViewModel = new AttendanceViewModel();
        add = true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated: " + (count++));
        getBinding().setViewmodel(attendanceViewModel);
    }

    public void setPresenter(AttendanceContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i(TAG, "onHiddenChanged: " + hidden);

        if (!hidden) {
            PictureManager.getInstance().load(getContext(), R.drawable.test3, getBinding().preview, false);
            if (presenter != null) {
                presenter.resetCount();
                presenter.setCheckAdmin(false);
                presenter.setmFaceDetectionView(this, getBinding().faceDetectionView);
                if (!presenter.isRuning()) {
                    presenter.startAttendance();
                }
            }
            attendanceViewModel.isShowPerson.set(View.INVISIBLE);
        }
    }

    private static final String TAG = "AttendanceFragment";

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
        if (add) {
            add = false;
            onHiddenChanged(add);
        }
        attendanceViewModel.personEntity.set(null);
    }

    @Override
    public void resetUser() {
        attendanceViewModel.isShowPerson.set(View.INVISIBLE);
    }

    @Override
    public void showImage(Bitmap bitmap) {
        if (getActivity() != null) {
            getBinding().preview.setImageBitmap(bitmap);
        }
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            resetUser();
        }
    };
    @Override
    public void showUser(final PersonEntity entity, boolean update) {
        if (entity == null) {
            ((SystemActivity) getActivity()).setTime(1);
        } else {
            ((SystemActivity) getActivity()).setTime(60);
            attendanceViewModel.isShowPerson.set(View.VISIBLE);

               attendanceViewModel.personEntity.set(entity);
            getBinding().getRoot().removeCallbacks(runnable);
            getBinding().getRoot().postDelayed(runnable,3000);
        }

    }
}
